// 该文件专门用于创建整个应用的路由器
import VueRouter from 'vue-router'
// 引入路由组件
import Login from '../pages/login/myLogin'
import Home from '../pages/home/Home.vue'
import myLink from '../pages/link/myLink'
import Admin from '../pages/admin/admin.vue'
import Atrticle from '../pages/articles'
import Blog from '../pages/blog'
import Search from '../pages/search'
import UpdateBlog from '../pages/updateBlog'
import keySearch from '../pages/keySearch'
import myPic from '../pages/pic/myPic'

const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(to) {
    return VueRouterPush.call(this, to).catch(err => err)
}

// 创建并暴露一个路由器
export default new VueRouter({
    routes: [
        {
            path: '/login',
            component: Login,
            meta: { show: false, },
            query: { token: '' }
        },
        {
            path: '/home',
            component: Home,
            meta: { show: true }
        },
        {
            path: '/mylink',
            component: myLink,
            meta: { show: false }
        },
        {
            path: '/admin',
            component: Admin,
            meta: { show: false, isAush: true },
            beforeEnter: (to, from, next) => {
                if (to.meta.isAush) {    // 判断是否需要鉴权
                    if (sessionStorage.getItem('token') === 'token') {
                        next()
                    } else {
                        alert('喂,站住!你不是铲屎官,不能进去!')
                    }
                }
            }
        },
        {
            path: '/article',
            component: Atrticle,
            meta: { show: false },
        },
        {
            path: '/blog',
            component: Blog,
            meta: { show: false },
        },
        {
            path: '/search',
            component: Search,
            meta: { show: false }

        },
        {
            path: '/updateblog',
            component: UpdateBlog,
            meta: { show: false }
        },
        {
            path: '/keysearch',
            component: keySearch,
            meta: { show: false }
        },
        {
            path:'/pic',
            component:myPic,
            meta: { show: false }
        },
        {
            // 重定向，在项目跑起来的时候，访问/立马让他定向到首页
            path: '*',
            redirect: '/home'
        },
    ],
    scrollBehavior(to, from, savedPosition) {
        return {
            x: 0,
            y: 0
        }
    }
})

